<?php

namespace app\api\controller;

use app\common\controller\Api;

use app\api\model\project\ProjectEvaluateZan as ProjectEvaluateZanModel;
use app\api\model\project\ProjectEvaluate as ProjectEvaluateModel;

/**
 * 评价点赞接口
 */
class Projectevaluatezan extends Api
{

    protected $noNeedRight = ['*'];

    //点赞
    public function add()
    {
        $evaluate_id = input('evaluate_id',0);
        if (empty($evaluate_id)) {
            $this->error('缺少参数评论id');
        }
        $data = [
            'user_id' => $this->auth->id,
            'evaluate_id' => $evaluate_id,
        ];

        $collect = new ProjectEvaluateZanModel();

        $info = ProjectEvaluateZanModel::where(['evaluate_id'=>$evaluate_id, 'user_id' => $this->auth->id])->find();
        if (empty($info)){
            $res = $collect->save($data);
            if ($res){
                ProjectEvaluateModel::where(['evaluate_id'=>$evaluate_id])->setInc('zan_num',1);
            }
            $this->success('点赞成功',$res);
        } else {
            $this->error('点赞失败');
        }
    }

    //删除
    public function delete()
    {
        $evaluate_id = input('evaluate_id',0);
        if (empty($evaluate_id)) {
            $this->error('缺少参数评论id');
        }

        $info = ProjectEvaluateZanModel::where(['evaluate_id'=>$evaluate_id, 'user_id' => $this->auth->id])->find();

        if (!empty($info)){
            $res = $info->delete();
            if ($res){
                ProjectEvaluateModel::where(['evaluate_id'=>$evaluate_id])->setDec('zan_num',1);
            }
            $this->success('取消成功',$res);
        } else {
            $this->error('取消失败');
        }
    }

    //是否点赞
    public function iszan()
    {
        $evaluate_id = input('evaluate_id',0);
        if (empty($evaluate_id)) {
            $this->error('缺少参数评论id');
        }

        $info = ProjectEvaluateZanModel::where(['evaluate_id'=>$evaluate_id, 'user_id' => $this->auth->id])->find();

        if (!empty($info)) {
            $this->success('',1);
        } else {
            $this->success('',0);
        }
    }


}